package com.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.Logger;
import org.apache.log4j.lf5.util.Resource;

import com.bean.ProductType;

public class Test {
	private SqlSessionFactory sqlSessionFactory=null;
	private SqlSession sqlSession=null;
	Logger logger=Logger.getLogger(Test.class);
	private void createSqlSessionFactor() throws IOException{
		InputStream inputStream=Resources.getResourceAsStream("SqlMapConfig.xml");
		sqlSessionFactory= new SqlSessionFactoryBuilder().build(inputStream);
	}
	private void findProductTypeById(){
		try {
			sqlSession= sqlSessionFactory.openSession();
			ProductType productType=sqlSession.selectOne("productType.findProductTypeById", 1);
			logger.info(productType);
		} catch (Exception e) {
			logger.error(e.getLocalizedMessage());
		}finally{
			sqlSession.close();	
		}
		
	}
	//findProductTypeByName
	private void findProductTypeByName(){
		try {
			sqlSession= sqlSessionFactory.openSession();
			List<ProductType>productType=sqlSession.selectList("productType.findProductTypeByName","product_type_name like ('手机')");
			logger.info(productType);
		} catch (Exception e) {
			logger.error(e.getLocalizedMessage());
		}finally{
			sqlSession.close();	
		}
		
	}
	//insertProductType
	private void insertProductType(){
		try {
			sqlSession= sqlSessionFactory.openSession();
			ProductType productType= new ProductType();
			productType.setProcuctTypeName("电子游戏");
			productType.setSort(1);
			int i= sqlSession.insert("productType.insertProductType",productType);
			System.out.println(i);
			sqlSession.commit();
		} catch (Exception e) {
			sqlSession.rollback();
			logger.error(e.getLocalizedMessage());
		}finally{
			sqlSession.close();	
		}
		
	}
	private void deleteProductTypeById(){
		try {
			sqlSession= sqlSessionFactory.openSession();
			int i= sqlSession.delete("productType.deleteProductTypeById",4);
			System.out.println(i);
			sqlSession.commit();
		} catch (Exception e) {
			sqlSession.rollback();
			logger.error(e.getLocalizedMessage());
		}finally{
			sqlSession.close();	
		}
		
	}
	private void updateProductTypeById(){
		try {
			sqlSession= sqlSessionFactory.openSession();
			ProductType productType= new ProductType();
			productType.setProcuctTypeName("游戏");
			productType.setSort(1);
			productType.setProductTypeId(6);
			int i= sqlSession.update("productType.updateProductTypeById",productType);
			System.out.println(i);
			sqlSession.commit();
		} catch (Exception e) {
			sqlSession.rollback();
			logger.error(e.getLocalizedMessage());
		}finally{
			sqlSession.close();	
		}
		
	}
	public static void main(String[] args) {
		Test test= new Test();
		try {
			test.createSqlSessionFactor();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//test.findProductTypeById();
		//test.findProductTypeByName();
		test.insertProductType();
		//test.deleteProductTypeById();
		//test.updateProductTypeById();
	}
}
